package com.example.service;

import com.example.mapper.JobRecruiterMapper;
import com.example.mapper.PosClickMapper;
import com.example.mapper.PostMapper;
import com.example.pojo.bo.HomePosterBO;
import com.example.pojo.dto.HomePosterDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/9
 */
@Service
public class HomePosterServiceImpl implements HomePosterService{
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private JobRecruiterMapper jobRecruiterMapper;
    @Autowired
    private SavePosClickService savePosClickService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PosClickMapper posClickMapper;

    @Override
    public List<HomePosterDTO> findAllPoster() {
        ListOperations los = redisTemplate.opsForList();
        if (redisTemplate.hasKey("indexRecommend")){
            List<HomePosterDTO> list = los.range("indexRecommend",0,-1);
            System.out.println(list);
            return list;
        }else {
            List<HomePosterDTO> list = loadDataAndCache();
            return list;
        }

    }
    @Override
    public List<HomePosterDTO> loadDataAndCache(){
        ListOperations los = redisTemplate.opsForList();
        List<HomePosterDTO> homePosterDTOS = new ArrayList<>();
        List<Integer> posIds = posClickMapper.findClickNumMax();
        for (Integer posId : posIds) {
            HomePosterBO homePosterBO = postMapper.findByPosId(posId);
            Integer jrId = homePosterBO.getJrId();
            String companyName = jobRecruiterMapper.findCompanyName(jrId);
            HomePosterDTO homePosterDTO = new HomePosterDTO();
            homePosterDTO.setJrId(jrId);
            homePosterDTO.setPosId(homePosterBO.getId());
            homePosterDTO.setCompanyName(companyName);
            homePosterDTO.setPosName(homePosterBO.getPosName());
            homePosterDTO.setPosType(homePosterBO.getPosType());
            if (null!=homePosterBO.getBackground()&&homePosterBO.getBackground()==0){
                homePosterDTO.setBackground("学历不限");
            } else if (null!=homePosterBO.getBackground()&&homePosterBO.getBackground()==1) {
                homePosterDTO.setBackground("本科以上");
            } else if (null!=homePosterBO.getBackground()&&homePosterBO.getBackground()==2) {
                homePosterDTO.setBackground("硕士以上");
            }else {
                homePosterDTO.setBackground("博士");
            }
            if (null!=homePosterBO.getPayFloor()&&homePosterBO.getPayFloor()>=1000){
                homePosterDTO.setPay(homePosterBO.getPayFloor()/1000+"k"+"-"+homePosterBO.getPayUpper()/1000+"k");
            }else {
                homePosterDTO.setPay(homePosterBO.getPayFloor()+"-"+homePosterBO.getPayUpper());
            }
            homePosterDTOS.add(homePosterDTO);
        }
        los.leftPushAll("indexRecommend",homePosterDTOS);
        redisTemplate.expire("indexRecommend",10,TimeUnit.SECONDS);
        return homePosterDTOS;
    }

    @Override
    public List<HomePosterDTO> findByPosId(Integer userId) {
        ListOperations los = redisTemplate.opsForList();
        if (redisTemplate.hasKey("userId_"+userId)){
            List<HomePosterDTO> list = los.range("userId_"+userId,0,-1);
            return list;
        }else {
            List<HomePosterDTO> list = findDataBySql(userId);
            System.out.println(list);
            if (list!=null&&list.size()>0){
                los.leftPushAll("userId_"+userId,list);
                redisTemplate.expire(userId,15, TimeUnit.MINUTES);
                System.out.println("====================");
                return list;
            }else {
                List<HomePosterDTO> list1 = findAllPoster();
                return list1;
            }

        }
    }
    public List<HomePosterDTO> findDataBySql(Integer userId){
        List<HomePosterDTO> homePosterDTOS = new ArrayList<>();
        List<Integer> posIds = savePosClickService.recommendPosId(userId);
        for (Integer posId : posIds) {
            HomePosterBO homePosterBO = postMapper.findByPosId(posId);
            Integer jrId = homePosterBO.getJrId();
            String companyName = jobRecruiterMapper.findCompanyName(jrId);
            HomePosterDTO homePosterDTO = new HomePosterDTO();
            homePosterDTO.setJrId(jrId);
            homePosterDTO.setPosId(homePosterBO.getId());
            homePosterDTO.setCompanyName(companyName);
            homePosterDTO.setPosName(homePosterBO.getPosName());
            homePosterDTO.setPosType(homePosterBO.getPosType());
            if (null!=homePosterBO.getBackground()&&homePosterBO.getBackground()==0){
                homePosterDTO.setBackground("学历不限");
            } else if (null!=homePosterBO.getBackground()&&homePosterBO.getBackground()==1) {
                homePosterDTO.setBackground("本科以上");
            } else if (null!=homePosterBO.getBackground()&&homePosterBO.getBackground()==2) {
                homePosterDTO.setBackground("硕士以上");
            }else {
                homePosterDTO.setBackground("博士");
            }
            if (null!=homePosterBO.getPayFloor()&&homePosterBO.getPayFloor()>=1000){
                homePosterDTO.setPay(homePosterBO.getPayFloor()/1000+"k"+"-"+homePosterBO.getPayUpper()/1000+"k");
            }else {
                homePosterDTO.setPay(homePosterBO.getPayFloor()+"-"+homePosterBO.getPayUpper());
            }
            homePosterDTOS.add(homePosterDTO);
        }
        return homePosterDTOS;
    }

}
